<?php
namespace Home\Model;
use Think\Model;
class ProductModel extends Model {
	public $table='zjy_product';
	public $page; //当前页
	public $perpage; //每页显示
	public $product_id; //ID
	public $pc_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $pc_ids; //获取分类ID下的子分类
	public $city_id; //搜索页面中的热门地区
	public $brand_id; //搜索页面中的品牌ID
	public $province_id; //搜索页面中的发货地
	public $search_sort;      //搜索中排序

	//获得列表 $page当前页 $perpage每页显示
	public function get_product_list()
	{
		//echo $this->keyword;exit;
		$page=$this->page;
		$perpage=$this->perpage;
		$start=($page-1)*$perpage;
		//搜索
		$where=' where 1 ';
		if($this->keyword){
			$where.=" and `product_name` LIKE '%".$this->keyword."%' ";
		}
		if($this->city_id){
			$where.=" and `city_id`=".$this->city_id." ";
		}
		if($this->brand_id){
			$where.=" and `brand_id`=".$this->brand_id." ";
		}
		if($this->province_id){
			$where.=" and `province_id`=".$this->province_id." ";
		}
		$order=" product_sort asc,product_id desc ";
		if($this->search_sort){
			$order=$this->search_sort;
		}
		$sql="select * from ".$this->table." ".$where." order by ".$order." limit $start,$perpage";
		//查询销量，条件需要调整
		if($order=='product_num desc'||$order=='product_num asc'){
			$where=' where 1 ';
			$order=' num desc,product_sort asc,product_id desc ';
			if($this->keyword){
				$where.=" and a.`product_name` LIKE '%".$this->keyword."%' ";
			}
			if($this->city_id){
				$where.=" and a.`city_id`=".$this->city_id." ";
			}
			if($this->brand_id){
				$where.=" and a.`brand_id`=".$this->brand_id." ";
			}
			if($this->province_id){
				$where.=" and a.`province_id`=".$this->province_id." ";
			}
			$sql="SELECT a.*,sum(b.product_num) as num FROM ".$this->table." as a LEFT JOIN  zjy_order_product as b ON a.product_id =b.product_id ".$where." group by a.product_id ORDER BY ".$order."  limit $start,$perpage";
		}
		//echo $sql;exit;

		//echo $where;exit;
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/

		$Model=new Model();
		$ProductClass=M("Product_class");
		$Member=M("Member");
		$OrderProduct=M("Order_product");
		$list=$Model->query($sql);
		//print_r($list);exit;
		foreach($list as $k=>$v){
			$list[$k]['pc_name']=$ProductClass->where('pc_id='.$v['pc_id'])->getField('pc_name');
			$list[$k]['member_truename']=$Member->where('member_id='.$v['seller_id'])->getField('member_truename');
			$list[$k]['sell_num']=$OrderProduct->where('product_id='.$v['product_id'])->sum('product_num');	//销量
		}
		//print_r($list);exit;
		return $list;
	}
	public function get_product_total()
	{
		//搜索
		$where='where 1 ';
		if($this->keyword){
			$where.="and `product_name` LIKE '%".$this->keyword."%'";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$query=$Model->query("select count(product_id) as total from `zjy_product` ".$where." ");
		return $query[0]['total'];
	}
	//取分类下所有子分类
	private function get_sub_pc_id($pc_id){
		$pc_id=intval($pc_id);
		$this->pc_ids.=','.$pc_id;
		$Model=M("ProductClass");
		$list=$Model->query("select `pc_id` from `zjy_product_class` where `pc_parent_id`=".$pc_id." order by pc_sort asc");
		if(is_array($list)){
			foreach($list as $k=>$v){
				$this->get_sub_pc_id($v['pc_id']);
			}
		}
		return $this->pc_ids;
	}
	//获取产品分类列表 全部放在一页 四级分类
	public function get_cat_list()
	{
		$Model=M("Product_class");
		$arr=array();
		$i=0;
		$list1=$Model->query("select * from `zjy_product_class` where `pc_parent_id`=0 order by `pc_sort` desc,`pc_id` desc");
		foreach($list1 as $k1=>$v1){
			$arr[$i]['pc_name']=$v1['pc_name'];
			$arr[$i]['pc_id']=$v1['pc_id'];
			$arr[$i]['pc_is_show']=$v1['pc_is_show'];
			$arr[$i]['pc_sort']=$v1['pc_sort'];
			$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v1['pc_top_id'])->getField('pc_name');
			$arr[$i]['pc_parent_name']=$Model->where('pc_id='.$v1['pc_parent_id'])->getField('pc_name');
			$i++;
			$list2=$Model->query("select * from `zjy_product_class` where `pc_parent_id`=".$v1['pc_id']." order by `pc_sort` desc,`pc_id` desc");
			foreach($list2 as $v2){
				$arr[$i]['pc_name']='--'.$v2['pc_name'];
				$arr[$i]['pc_id']=$v2['pc_id'];
				$arr[$i]['pc_is_show']=$v2['pc_is_show'];
				$arr[$i]['pc_sort']=$v2['pc_sort'];
				$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v2['pc_top_id'])->getField('pc_name');
				$arr[$i]['pc_parent_name']=$Model->where('pc_id='.$v2['pc_parent_id'])->getField('pc_name');
				$i++;
				$list3=$Model->query("select * from `zjy_product_class` where `pc_parent_id`=".$v2['pc_id']." order by `pc_sort` desc,`pc_id` desc");
				foreach($list3 as $v3) {
					$arr[$i]['pc_name'] ='----'.$v3['pc_name'];
					$arr[$i]['pc_id']=$v3['pc_id'];
					$arr[$i]['pc_is_show']=$v3['pc_is_show'];
					$arr[$i]['pc_sort']=$v3['pc_sort'];
					$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v3['pc_top_id'])->getField('pc_name');
					$arr[$i]['pc_parent_name']=$Model->where('pc_id='.$v3['pc_parent_id'])->getField('pc_name');
					$i++;
					$list4=$Model->query("select * from `zjy_product_class` where `pc_parent_id`=".$v3['pc_id']." order by `pc_sort` desc,`pc_id` desc");
					foreach($list4 as $v4) {
						$arr[$i]['pc_name'] = '------' . $v4['pc_name'];
						$arr[$i]['pc_id'] = $v4['pc_id'];
						$arr[$i]['pc_is_show']=$v4['pc_is_show'];
						$arr[$i]['pc_sort']=$v4['pc_sort'];
						$arr[$i]['pc_top_name']=$Model->where('pc_id='.$v4['pc_top_id'])->getField('pc_name');
						$arr[$i]['pc_parent_name']=$Model->where('pc_id='.$v4['pc_parent_id'])->getField('pc_name');
						$i++;
					}
				}
			}
		}
		return $arr;
	}
	//提交
	public function product_add_post()
	{
		$Model=M("Product");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//获取信息
	public function get_product_info()
	{
		$product_id=intval($this->product_id);
		$Model=M("Product");
		$info=$Model->where('product_id='.$product_id)->find();
		return $info;
	}
	//编辑提交
	public function product_edit_post()
	{
		$Model=M("Product");
		$condition['product_id'] = $this->post['product_id'];
		$data=$this->post;
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function delete_product()
	{
		$ids=$this->ids;
		$Model=M("Product");
		foreach($ids as $v){
			$condition['product_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//批量更新排序
	public function update_product_sort()
	{
		$ids=$this->ids;
		$sorts=$this->sorts;
		$Model=M("Product");
		foreach($ids as $k=>$v){
			$data['product_sort']=$sorts[$k];
			$condition['product_id']=$v;
			$result=$Model->where($condition)->save($data);
			if($result === false) {
				return false;
			}
		}
		return true;
	}
}